package com.company.managesystem.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.company.managesystem.R;
import com.company.managesystem.common.Apis;
import com.company.managesystem.common.DataFetch;
import com.company.managesystem.model.UpdatPdState;
import com.company.managesystem.util.SharePreferenceUtil;

/**
 * 未完成工单详情
 * Created by lqy on 2017/6/29.
 */

public class UnfinishedActivity extends BaseActivity {
    private TextView workOrderText, dateText, addressText, contentText, nameText;
    private Button submitButton, cancleButton;
    private String workOrder, date, address, content, name, status;
    private int state;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_unfinished);
    }

    @Override
    protected void initView() {
        setToobarBackIcon(R.mipmap.ic_arrow_back);
        setToolBarTitle("工单详情");
        workOrderText = (TextView) findViewById(R.id.workorder_text);
        dateText = (TextView) findViewById(R.id.time_text);
        addressText = (TextView) findViewById(R.id.repair_address_text);
        contentText = (TextView) findViewById(R.id.repair_content_text);
        nameText = (TextView) findViewById(R.id.service_man_text);
        submitButton = (Button) findViewById(R.id.submit_button);
        cancleButton = (Button) findViewById(R.id.cacel_button);
    }

    @Override
    protected void initData() {
        name = new SharePreferenceUtil(this).getName();
        Bundle bundle = this.getIntent().getExtras();
        status = bundle.getString("status");
        if (status.equals("unfinished")) {
            workOrder = bundle.getString("unfinished_code");
            date = bundle.getString("unfinished_time");
            address = bundle.getString("unfinished_address");
            content = bundle.getString("unfinished_content");
            submitButton.setText("接单");
            cancleButton.setText("退回");
            submitButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getData(2010);
                }
            });
            cancleButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDialog();
                }
            });
        } else if (status.equals("repairing")) {
            workOrder = bundle.getString("repairing_code");
            date = bundle.getString("repairing_time");
            address = bundle.getString("repairing_address");
            content = bundle.getString("repairing_content");
            submitButton.setText("完成");
            cancleButton.setVisibility(View.GONE);
            submitButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDialog();
                }
            });
        }
        workOrderText.setText(workOrder);
        dateText.setText(date);
        addressText.setText(address);
        contentText.setText(content);
        nameText.setText(name);
    }

    private void getData(int state) {
        String url = "";
        if(status.equals("unfinished")) {
            url = Apis.api_state + "?pdCode=" + workOrder + "&state=" + state + "&jsrCode=" + new SharePreferenceUtil(this).getJsrCode();
        } else if (status.equals("repairing")) {
            url = Apis.api_finishedState + "?bxCode=" + workOrder + "&state=" + state;
        }

        DataFetch dataFetch = new DataFetch<UpdatPdState>(this, url, false, UpdatPdState.class) {
            @Override
            protected void onDataLoaded(final UpdatPdState data) {
                super.onDataLoaded(data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (data.getState() == 1) {
                            Toast.makeText(UnfinishedActivity.this,"操作成功",Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(UnfinishedActivity.this,"操作失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

            @Override
            protected void onDataLoadError() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(UnfinishedActivity.this, "网络错误，请重新加载！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        dataFetch.LoadingData(false);
    }

    private void showDialog(){
        String message = "";
        if (status.equals("unfinished")) {
            message = "确定要退回么？";
            state = 2020;
        } else if (status.equals("repairing")) {
            message = "确定已经完成？";
            state = 1030;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(UnfinishedActivity.this);
        builder.setTitle("提示！");
        builder.setMessage(message);
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                getData(state);
            }
        });
        builder.setPositiveButton("取消",null);
        builder.show();

    }

}
